package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.sys.bean.SysProductPrice;
import com.fzrskj.cloud.free.sys.dao.SysProductPriceDao;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import ms.core.object.CObject;
import ms.core.tool.StrTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysProductPriceUtils extends CObject{

	//删除产品价格(根据产品编号)
	public int delSysProductPrice(Map<String, Object> cdt){
		SysProductPriceDao dao=new SysProductPriceDao();
		int i = dao.delSysProductPrice(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	//删除产品价格(根据产品编号) 避免删除按注册商设置的价格
	public int delSysProductPrice(String productCode){
		SysProductPriceDao dao=new SysProductPriceDao();
		int i = dao.delSysProductPrice(productCode);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	//删除产品价格(非默认)
	public int delSysDomainProductPriceNotDefaule(String productClass, String productCode){
		SysProductPriceDao dao=new SysProductPriceDao();
		int i = dao.delSysDomainProductPriceNotDefaule(productClass, productCode);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	//添加域名产品价格
	public boolean addDomainProductPrice(String productCode,String productClass,String priceItem){
		JSONArray json = JSONArray.fromObject(priceItem);
		if(json.size()<=0) {
			return false;
		}
		for(int i=0;i<json.size();i++){
			JSONObject job = json.getJSONObject(i);
			SysProductPrice entity=new SysProductPrice();
        	entity.setProductClass(productClass);
        	entity.setProductCode(productCode);
        	entity.setChargeId(job.getInt("chargeId"));
        	entity.setLevelCode(job.getString("levelCode"));
        	if(job.containsKey("regType")){
				entity.setRegType(job.getInt("regType"));
			}
        	entity.setPrice1(StrTool.strToIntDef(job.getString("price1"), -1));
        	entity.setPrice2(StrTool.strToIntDef(job.getString("price2"), -1));
        	entity.setPrice3(StrTool.strToIntDef(job.getString("price3"), -1));
        	entity.setPrice4(StrTool.strToIntDef(job.getString("price4"), -1));
        	entity.setPrice5(StrTool.strToIntDef(job.getString("price5"), -1));
        	entity.setPrice6(StrTool.strToIntDef(job.getString("price6"), -1));
        	entity.setPrice7(StrTool.strToIntDef(job.getString("price7"), -1));
        	entity.setPrice8(StrTool.strToIntDef(job.getString("price8"), -1));
        	entity.setPrice9(StrTool.strToIntDef(job.getString("price9"), -1));
        	entity.setPrice10(StrTool.strToIntDef(job.getString("price10"), -1));
    		SysProductPriceDao dao=new SysProductPriceDao();
        	dao.addSysProductPrice(entity);
	    	dao.dispose();
		}

		return true;
	}

	//获取产品价格列表
	public List<SysProductPrice> getSysProductPriceList(String productCode){
		SysProductPriceDao dao=new SysProductPriceDao();
		List<SysProductPrice> list=dao.getSysProductPriceList(productCode);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	//获取产品价格
	public int getProductPrice(int year, Map<String, Object> cdt){
		int price=0;
		SysProductPriceDao dao=new SysProductPriceDao();
		price=dao.getProductPrice(year,cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return price;

	}

	//获取域名产品价格
	public int getDomainProductPrice(int year, Map<String, Object> cdt) {
		//是否允许按域名注册商设置价格开关
		int price = -1;
		SysProductPriceDao dao = new SysProductPriceDao();
		price = dao.getProductPrice(year, cdt);

		if (price <= 0) {
			cdt.remove("regType");
			price = dao.getProductPrice(year, cdt);
		}
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return price >= 0 ? price : -1;
	}

	//获取产品可购买时长
	public List<Integer> getProductBuyTime(Map<String, Object> cdt){
		int userId=StrTool.strToIntDef(cdt.get("userId").toString(), 0);
		UserAccountUtils uaUtils=new UserAccountUtils();
		UserAccount ua=uaUtils.queryUserAccountByUid(userId);

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productCode", cdt.get("productCode"));
		param.put("chargeId", cdt.get("chargeId"));
		param.put("levelCode", ua.getLevelCode());
//		DicParamUtils dpUtils = new DicParamUtils();
//		String selectAgent = dpUtils.getParamValue("selectAgent", 1);
//		if (!DomainPriceByRegTypeEnum.DOMAIN_PRICE_BY_REG_TYPE_OK.getVal().equals(selectAgent)){
//			param.remove("regType");
//		}
		SysProductPriceDao dao=new SysProductPriceDao();
		SysProductPrice view=dao.getProductBuyTime(param);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();

		List<Integer> time = new ArrayList<Integer>();
		if (view.getPrice1() != null && view.getPrice1() != 0) {
			time.add(1);
		}
		if(view.getPrice2() != null && view.getPrice2()!=0) {
			time.add(2);
		}
		if(view.getPrice3() != null && view.getPrice3()!=0){
			time.add(3);
		}
		if(view.getPrice4() != null && view.getPrice4()!=0) {
			time.add(4);
		}
		if(view.getPrice5() != null && view.getPrice5()!=0) {
			time.add(5);
		}
		if(view.getPrice6() != null && view.getPrice6()!=0){
			time.add(6);
		}
		if(view.getPrice7() != null && view.getPrice7()!=0) {
			time.add(7);
		}
		if(view.getPrice8() != null && view.getPrice8()!=0) {
			time.add(8);
		}
		if(view.getPrice9() != null && view.getPrice9()!=0){
			time.add(9);
		}
		if(view.getPrice10() != null && view.getPrice10()!=0) {
			time.add(10);
		}
		return time;
	}
}
